package com.minisoft.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class LineCount {

    public static void main(String[] args) {
//        String folderPath = "C:\\Users\\31594\\IdeaProjects\\gf-pre-sale-center"; // 替换为你的文件夹路径
//        String folderPath = "D:\\git\\mobile-case-miniprogram-dev\\src"; // 替换为你的文件夹路径
        String folderPath = "D:\\git\\gf-call-mgt-front\\src"; // 替换为你的文件夹路径
        String fileExtension = ".js"; // 替换为你的文件后缀

        int totalLines = countLinesInFolder(folderPath, fileExtension);
        System.out.println("总行数: " + totalLines);
    }

    public static int countLinesInFolder(String folderPath, String fileExtension) {
        File folder = new File(folderPath);
        if (!folder.exists() || !folder.isDirectory()) {
            throw new IllegalArgumentException("指定路径不是一个有效的文件夹路径。");
        }

        int totalLines = 0;
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile() && file.getName().endsWith(fileExtension)) {
                    totalLines += countLinesInFile(file);
                } else if (file.isDirectory()) {
                    totalLines += countLinesInFolder(file.getAbsolutePath(), fileExtension);
                }
            }
        }

        return totalLines;
    }

    public static int countLinesInFile(File file) {
        int lines = 0;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            while (reader.readLine() != null) {
                lines++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return lines;
    }
}
